from tornado import gen
from base.BaseHandler import AuthHandler
from base.Models import Company


class CompanyHandler(AuthHandler):
    @gen.coroutine
    def get(self):
        result = self.result()
        try:
            data = self.get_params()
            is_all = int(data.get("is_all", 0))
            page = int(data.get("page", 1))
            limit = int(data.get("limit", 10))
            company_name = data.get("company_name")
            data_list = []
            company_objects = self.session.query(Company).filter_by(user_id=self.user_id, is_delete=0)

            if is_all == 1:
                # 获取全量数据
                company_data = company_objects.all()
                for raw in company_data:
                    arr = {
                        "id": raw.id,
                        "name": raw.company_name
                    }
                    data_list.append(arr)
            else:
                # 获取分页数据
                if company_name:
                    company_objects = company_objects.filter(Company.company_name.like(f"%{company_name}%"))
                company_data = company_objects.order_by(Company.create_time.desc()).limit(limit).offset((page-1)*limit).all()
                for raw in company_data:
                    arr = {
                        "id": raw.id,
                        "name": raw.company_name,
                        "boss": raw.company_boss,
                        "address": raw.company_address,
                        "on_the_job": raw.on_the_job,
                        "position": raw.position,
                        "remark": raw.remark,
                        "create_time": str(raw.create_time)
                    }
                    data_list.append(arr)

            data = {
                "data_count": company_objects.count(),
                "data_list": data_list
            }
            result["status"] = 1
            result["msg"] = "数据获取成功！"
            result["data"] = data
        except Exception as e:
            self.logger.error(e)
            result = self.check_except_zh(e)
        self.write(result)

    @gen.coroutine
    def post(self):
        result = self.result()
        try:
            data = self.get_body()
            company_name = self.data_not_null(data, "company_name", "请输入公司名称！")
            company_boss = self.data_not_null(data, "company_boss", "请输入公司管理人名称！")
            company_address = self.data_not_null(data, "company_address", "请输入公司地址！")
            on_the_job = self.data_not_null(data, "on_the_job", "请选择是否在职！")
            position = self.data_not_null(data, "position", "请输入职位！")
            remark = data.get("remark")

            new_company = Company(user_id=self.user_id, company_name=company_name, company_boss=company_boss,
                                  company_address=company_address, on_the_job=int(on_the_job), position=position,
                                  remark=remark)
            self.session.add(new_company)
            self.record_user_operation(self.user_id, self.user_name, todo=f"新增公司-{company_name}")
            self.session.commit()
            result["status"] = 1
            result["msg"] = "新增公司成功！"
        except Exception as e:
            self.session.rollback()
            self.logger.error(e)
            result = self.check_except_zh(e)
        self.write(result)

    @gen.coroutine
    def put(self):
        result = self.result()
        try:
            data = self.get_body()
            company_id = self.data_not_null(data, "company_id", "请选择公司！")
            company_name = self.data_not_null(data, "company_name", "请输入公司名称！")
            company_boss = self.data_not_null(data, "company_boss", "请输入公司管理人名称！")
            company_address = self.data_not_null(data, "company_address", "请输入公司地址！")
            on_the_job = self.data_not_null(data, "on_the_job", "请选择是否在职！")
            position = self.data_not_null(data, "position", "请输入职位！")
            remark = data.get("remark")

            company_verify = self.session.query(Company).filter_by(id=int(company_id), user_id=self.user_id, is_delete=0).first()
            if not company_verify:
                self.data_except("请确认公司是否存在！")
            if company_verify.company_name != company_name:
                old_name = company_verify.company_name
                company_verify.company_name = company_name
                self.record_user_operation(self.user_id, self.user_name, todo=f"更新公司信息-{old_name}-公司名称变更为[{company_name}]-{company_id}")
            if company_verify.company_boss != company_boss:
                company_verify.company_boss = company_boss
                self.record_user_operation(self.user_id, self.user_name, todo=f"更新公司信息-{company_verify.company_name}-公司管理人变更为[{company_boss}]-{company_id}")
            if company_verify.company_address != company_address:
                company_verify.company_address = company_address
                self.record_user_operation(self.user_id, self.user_name, todo=f"更新公司信息-{company_verify.company_name}-公司地址变更为[{company_address}]-{company_id}")
            if company_verify.on_the_job != int(on_the_job):
                company_verify.on_the_job = int(on_the_job)
                on_job = "在职" if int(on_the_job)==1 else "离职"
                self.record_user_operation(self.user_id, self.user_name, todo=f"更新公司信息-{company_verify.company_name}-变更为[{on_job}]-{company_id}")
            if company_verify.position != position:
                company_verify.position = position
                self.record_user_operation(self.user_id, self.user_name, todo=f"更新公司信息-{company_verify.company_name}-职位信息变更为[{position}]-{company_id}")
            company_verify.remark = remark
            self.session.commit()
            result["status"] = 1
            result["msg"] = "更新公司信息成功！"
        except Exception as e:
            self.session.rollback()
            self.logger.error(e)
            result = self.check_except_zh(e)
        self.write(result)

    @gen.coroutine
    def delete(self):
        result = self.result()
        try:
            data = self.get_body()
            company_id = self.data_not_null(data, "company_id", "请选择公司！")

            company_verify = self.session.query(Company).filter_by(id=int(company_id), user_id=self.user_id, is_delete=0).first()
            if not company_verify:
                self.data_except("请确认公司是否存在！")
            company_verify.is_delete = 1
            self.record_user_operation(self.user_id, self.user_name, todo=f"删除公司信息-{company_verify.company_name}-{company_id}")
            self.session.commit()
            result["status"] = 1
            result["msg"] = "删除公司信息成功！"
        except Exception as e:
            self.session.rollback()
            self.logger.error(e)
            result = self.check_except_zh(e)
        self.write(result)

